
function factory (UserModel) {
  return function (req, res) {
    var body = req.body;
    UserModel.findOne({user:body.user}, function (err, user) {
      if (!user) {
        UserModel.findOne({email: body.user}, function (err, user) {
          if (!user) {
            res.send('-1')
          }else {
            if ((user.email !== body.user ) || user.password !== body.password) {
              res.send('0')
            }else {
              user.save(function (err,user) {
                res.send(user)
              })
            }
          }
        })
      }else {
        if ((user.user !== body.user ) || user.password !== body.password) {
          res.send('0')
        }else {
          user.save(function (err,user) {
            res.send(user)
          })
        }
      }
    })
  }

}

module.exports = factory